using System.Xml.Linq;
using Engine;
using Engine.Graphics;
using Game;

namespace SCIENEW {
    public class HearthFurnaceWidget : CanvasWidget {
        public GridPanelWidget m_inventoryGrid;

        public ComponentHearthFurnace m_componentCraftingTable;

        public LabelWidget m_furnaceLabel;

        public TankWidget m_ironTank;

        public TankWidget m_steelTank;

        public GridPanelWidget m_catGrid;

        public FireWidget m_fire;

        public ValueBarWidget m_progress;

        public HearthFurnaceWidget(IInventory inventory, ComponentHearthFurnace componentCraftingTable) {
            m_componentCraftingTable = componentCraftingTable;
            XElement node = ContentManager.Get<XElement>("Widgets/HearthFurnaceWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            m_furnaceLabel = Children.Find<LabelWidget>("HearthFurnaceLabel");
            m_ironTank = Children.Find<TankWidget>("IronTank");
            m_steelTank = Children.Find<TankWidget>("SteelTank");
            m_catGrid = Children.Find<GridPanelWidget>("CatGrid");
            m_fire = Children.Find<FireWidget>("Fire");
            m_progress = Children.Find<ValueBarWidget>("Progress");
            int num = 10;
            for (int i = 0; i < m_inventoryGrid.RowsCount; i++) {
                for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++) {
                    var inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(inventory, num++);
                    m_inventoryGrid.Children.Add(inventorySlotWidget);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }
            int index = 0;
            for (int i = 0; i < m_catGrid.RowsCount; i++) {
                for (int j = 0; j < m_catGrid.ColumnsCount; j++) {
                    var inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(componentCraftingTable, index++);
                    m_catGrid.Children.Add(inventorySlotWidget);
                    m_catGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }
            m_ironTank.AssignTank(m_componentCraftingTable, 0);
            m_steelTank.AssignTank(m_componentCraftingTable, 1);
        }

        public override void Update() {
            m_furnaceLabel.Text = LanguageControl.GetContentWidgets("HearthFurnaceWidget", 1) + (m_componentCraftingTable.IsIntegrity ? "(已成型)" : "");
            m_fire.ParticlesPerSecond = m_componentCraftingTable.IsWorking ? 24 : 0;
            if (!m_componentCraftingTable.IsAddedToProject) {
                ParentWidget.Children.Remove(this);
            }
        }
    }
}